# -*- coding:utf-8 -*-
# @FileName  :dhcc_per_1_report.py
# @Time      :2020/11/17 14:52
# @Author    :Zhoujirong
# @Desc      : 页面查询报告主流程
import parameterized
import datetime,time
from common.log import Log
from biz.flow.ent_report import EntReportRequest
from biz.cases.base_case import BaseCase
from common.params_operator import Param
from common.utils import get_redis
from common.utils import download

class TestEntReport(BaseCase):
    '''企业查询报告主流程测试用例'''

    data_file_name = 'ent_report.yaml'

    @classmethod
    def setUpClass(cls):
        cls.redis = get_redis()
        cls.cookie = BaseCase().get_token()

    def test_a_info_create_ent(self):
        '''添加客户信息'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = self.file_data.get('queryFormat')
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        info_check = self.file_data.get('info_check')
        ret = EntReportRequest.ent_info(enterpriseName,signCode,uniformSocialCredCode,orgInstCode,
                                        assocbsnssData,note,queryFormat,resultType,qryReason,
                                        queryControlWarningType,serialNumber,self.cookie)
        Log.info(ret)
        self.assertEqual(info_check, ret.resultMsg, '接口调用失败')

    def ent_archive_create(self):
        '''新建授权信息'''
        archiveType = self.file_data.get('archiveType')
        Num = self.file_data.get('Num')
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        orgInstCode = self.file_data.get('orgInstCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        startDate = datetime.date.today()
        expireDate = datetime.date.today()
        note = self.file_data.get('note')
        paperFileId = self.file_data.get('paperFileId')
        fileDescribe = self.file_data.get('fileDescribe')
        ret = EntReportRequest.ent_archive_create(archiveType,Num,enterpriseName,signCode,orgInstCode,
                           uniformSocialCredCode,startDate,expireDate,note,
                           paperFileId,fileDescribe,self.cookie)
        Log.info(ret)
        self.assertIsNotNone(ret.msg)
        return ret.msg

    def ent_save_paper(self):
        '''上传授权档案'''
        paperFileId = self.file_data.get('paperFileId')
        fileDescribe = self.file_data.get('fileDescribe')
        archiveId = self.ent_archive_create()
        ret = EntReportRequest.ent_save_paper(paperFileId,fileDescribe,archiveId,self.cookie)
        Log.info(ret)
        self.assertEqual('00000000',ret.code)
        return archiveId

    def ent_checkinfo(self):
        '''检查是够需要授权'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.ent_save_paper()
        ret = EntReportRequest.ent_checkinfo(enterpriseName,signCode,uniformSocialCredCode,
                      orgInstCode,assocbsnssData,note,queryFormat,resultType,
                      qryReason,queryControlWarningType,serialNumber,archiveId,self.cookie)
        Log.info(ret)
        self.assertEqual('inquire/checkInfo',ret.url)
        return archiveId

    def ent_inquire(self):
        '''验证本地是够有报告'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.ent_checkinfo()
        rekUser = self.file_data.get('rekUser')
        rekPasword = self.file_data.get('rekPasword')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = EntReportRequest.ent_inquire(enterpriseName,
                    signCode,
                    uniformSocialCredCode,
                    orgInstCode,
                    assocbsnssData,
                    note,
                    queryFormat,
                    resultType,
                    qryReason,
                    queryControlWarningType,
                    serialNumber,
                    archiveId,
                    rekUser,
                    rekPasword,
                    checkWay,
                    autharchiveId,self.cookie)
        Log.info(ret)
        self.assertIsNotNone(ret.resultMsg)
        return ret.checkId,archiveId

    @parameterized.parameterized.expand(Param.from_excel('cqs_ent_report_data.xls'))
    def test_ent_report_params(self,enterpriseName,
                                signCode,
                                uniformSocialCredCode,
                                orgInstCode,
                                assocbsnssData,
                                note,
                                queryFormat,
                                resultType,
                                qryReason,
                                queryControlWarningType,
                                serialNumber,
                                rekUser,
                                rekPasword,
                                checkWay,check,desc):
        '''提交异步请求'''
        self._testMethodDoc = desc
        archiveId = self.ent_checkinfo()
        autharchiveId =  archiveId
        ret = EntReportRequest.ent_inquire(enterpriseName,signCode,
                                uniformSocialCredCode,orgInstCode,
                                assocbsnssData,note,
                                queryFormat,resultType,
                                qryReason,queryControlWarningType,
                                serialNumber,archiveId,
                                rekUser,rekPasword,
                                checkWay,autharchiveId,self.cookie)
        Log.info(ret)
        self.assertIn(check,ret.resultMsg,'实际结果和预期结果不符')

    def show_report(self):
        '''人行查报告'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        checkId,archiveId = self.ent_inquire()
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = EntReportRequest.ent_show_report(enterpriseName,
                                                signCode,
                                                uniformSocialCredCode,
                                                orgInstCode,
                                                assocbsnssData,
                                                note,
                                                queryFormat,
                                                resultType,
                                                qryReason,
                                                queryControlWarningType,
                                                serialNumber,
                                                archiveId,
                                                autharchiveId,
                                                checkId,
                                                rekUser,
                                                checkWay,self.cookie)
        Log.info(ret)
        self.assertIsNotNone(ret.url)
        return ret.url

    def test_b_reveal_report(self):
        '''展示报告'''
        recordId = self.show_report()[30:]
        ret = EntReportRequest.ent_reveal_report(recordId, self.cookie)
        Log.info(ret)
        name = '企业信用报告dhcc_ent_1_reportDreveal_report%s.html'%time.strftime('%Y%m%d%H%M%S',time.localtime())
        size = download(name,ret)
        Log.info('文件大小为:%s'%size)
        self.assertNotEqual(0,size)

    def inquire_local(self):
        '''本地有报告'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.ent_checkinfo()
        rekUser = self.file_data.get('rekUser')
        rekPasword = self.file_data.get('rekPasword')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = EntReportRequest.ent_inquire(enterpriseName,
                                           signCode,
                                           uniformSocialCredCode,
                                           orgInstCode,
                                           assocbsnssData,
                                           note,
                                           queryFormat,
                                           resultType,
                                           qryReason,
                                           queryControlWarningType,
                                           serialNumber,
                                           archiveId,
                                           rekUser,
                                           rekPasword,
                                           checkWay,
                                           autharchiveId, self.cookie)
        Log.info(ret)
        self.assertEqual('10401',ret.resCode)
        self.assertEqual('本地存在信用报告，是否进行展示?',ret.resMsg)
        return ret.checkId, archiveId

    def test_c_local_report(self):
        '''查询本地报告'''
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        checkId, archiveId = self.ent_inquire()
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = EntReportRequest.local_report(enterpriseName,
                                               signCode,
                                               uniformSocialCredCode,
                                               orgInstCode,
                                               assocbsnssData,
                                               note,
                                               queryFormat,
                                               resultType,
                                               qryReason,
                                               queryControlWarningType,
                                               serialNumber,
                                               archiveId,
                                               autharchiveId,
                                               checkId,
                                               rekUser,
                                               checkWay, self.cookie)
        Log.info(ret)
        self.assertEqual('00',ret.resultCode)
        self.assertEqual('OK',ret.resultMsg)
        self.assertIn('inquire/revealReport?recordId=',ret.url)

    def test_d_prevalidate(self):
        '''查询预校验'''
        pre = self.file_data.get('pre')
        ret = EntReportRequest.prevalidate(pre,self.cookie)
        Log.info(ret)
        self.assertEqual('C0000',ret.queryControlWarningType)
        self.assertEqual('10000',ret.resCode)
        self.assertEqual('查询控制验证通过',ret.resMsg)









if __name__ == "__main__":
    pass